﻿// 参见： https://ai-cyber-security.feishu.cn/docx/Krk0davcQoDQcBx0ZQAc2pOUnTh

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PythonInvokerTest
{
    internal static class Program
    {
        static bool debug_Mode = false;
        public static string VersionInfo = "0.9.0";
        public static string ReleaseDate = "2024/09/25";

        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            // 允许跨线程数据访问
            Control.CheckForIllegalCrossThreadCalls = false;

            if (debug_Mode)
            {
                string s = "'--output-file', type = str, default = 'none_differ_Data.csv', help = 'Path to, the \'output CSV file.'";
                var args = ExtractArgument(s);
                Console.WriteLine($"head: {args[0]}");
                Console.WriteLine($"type: {args[1]}");
                Console.WriteLine($"deft: {args[2]}");
                Console.WriteLine($"help: {args[3]}");
                return;
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }

        // 函数定义：parser = argparse.ArgumentParser(description='Find unique vulnerability descriptions using embeddings.')
        // 参数定义：parser.add_argument('--data-file', type = str, default = 'final_data.json', help = 'Path to the JSON file containing vulnerability data.')
        // 参数定义：parser.add_argument('--output-file', type = str, default = 'none_differ_Data.csv', help = 'Path to the output CSV file.')

        public static string[] ExtractArgument(string s)
        {
            bool in_str = false;
            int bracket_level = 0;
            List<int> breaks = new List<int>();
            for (int i = 0; i < s.Length; i++)
            {
                char c = s[i];
                switch (c)
                {
                    case '(':
                        if (bracket_level == 0)
                            bracket_level++;
                        break;

                    case '\'':
                        in_str = !in_str;
                        break;

                    case ',':
                        if (!in_str)
                            breaks.Add(i);
                        break;
                }
            }

            StringBuilder sb = new StringBuilder();
            foreach (var b in breaks)
            {
                sb.AppendLine(b + ":" + s[b]);
            }
            breaks.Insert(0, -1);
            breaks.Add(s.Length);

            List<string> sections = new List<string>();
            for (int i = 0; i < breaks.Count - 1; i++)
            {
                int start = breaks[i] + 1;
                int size = breaks[i + 1] - start;
                sections.Add(s.Substring(start, size).Trim());
            }
            string head = "";
            string type = "";
            string default_value = "";
            string help = "";
            for (int i = 0; i < sections.Count; i++)
            {
                var si = sections[i];
                if (si.Contains("type"))
                {
                    type = si.Split('=')[1].Trim();
                }
                else if (si.Contains("default"))
                {
                    int p = si.IndexOf('=');
                    default_value = si.Substring(p + 1).Trim().Trim('\"').Trim('\'');
                }
                else if (si.Contains("help"))
                {
                    int p = si.IndexOf('=');
                    help = si.Substring(p + 1).Trim().Trim('\"').Trim('\'');
                }
                else
                {
                    head = si.Trim().Trim('\'').Trim('\"');
                }

            }


            string[] ret = new string[4];
            ret[0] = head;
            ret[1] = type;
            ret[2] = default_value;
            ret[3] = help;
            return ret;
        }
    }
}
